package com.sy.service.allocate.impl;

import com.alibaba.excel.EasyExcel;
import com.sy.bean.allocate.Allocate;
import com.sy.bean.pucharse.AuditStatus;
import com.sy.bean.pucharse.excel.AllocateExc;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.common.exception.BusinessException;
import com.sy.common.utils.DateUtils;
import com.sy.mapper.allocate.AllocateDetailMapper;
import com.sy.mapper.allocate.AllocateMapper;
import com.sy.service.allocate.AllocateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

@Service
public class AllocateServiceImpl extends BaseServiceImpl<Allocate, AllocateMapper> implements AllocateService {
    @Autowired
    AllocateMapper allocateMapper;
    @Autowired
    AllocateDetailMapper allocateDetailMapper;
    @Autowired
    HttpServletRequest request;

    @Transactional
    public int batchDelete(List<Integer> ids) {
        int rs = allocateMapper.batchDelete(ids);
        if (rs < 1)
            throw new BusinessException("删除调拨单失败");
        rs = allocateDetailMapper.batchDelete(ids);
        return 1;
    }

    @Transactional
    @Override
    public int save(Allocate entity) {
        entity.setCode(DateUtils.setCode("DB"));
        int rs = allocateMapper.save(entity);
        if (rs < 1)
            throw new BusinessException("新增调拨单失败");
        if (entity.getAllocateDetailList().size() < 1)
            throw new BusinessException("调拨单详情不能为空!");
        rs = allocateDetailMapper.batchSave(entity.getId(), entity.getAllocateDetailList());
        if (rs < 1)
            throw new BusinessException("新增调拨单详情失败");
        updateNum(entity);
        return 1;
    }

    @Override
    public String exportAll(Map map) {
        List<AllocateExc> list = allocateMapper.exportAll(map);
        String root = request.getServletContext().getRealPath("/") + "/excel";
        File file = new File(root);
        if (!file.exists())
            file.mkdirs();
        String newFileName = file.getAbsolutePath() + "/调拨单列表.xlsx";
        EasyExcel.write(newFileName, AllocateExc.class)
                .sheet("调拨单列表")
                .doWrite(() -> {
                    return list;
                });
        return "/excel/调拨单列表.xlsx";
    }

    @Transactional
    @Override
    public int update(Allocate entity) {
        int rs = allocateMapper.update(entity);
        if (rs < 1)
            throw new BusinessException("调拨单修改失败");
        rs = allocateDetailMapper.deleteByTbId(entity.getId());
        if (rs < 1)
            throw new BusinessException("调拨单修改失败");
        rs = allocateDetailMapper.batchSave(entity.getId(), entity.getAllocateDetailList());
        if (rs < 1)
            throw new BusinessException("调拨单修改失败");
        updateNum(entity);
        return 1;
    }

    @Transactional
    public void updateNum(Allocate entity) {
        try {
            entity.getAllocateDetailList().forEach(o -> {
                allocateMapper.updateInNum(o.getNum(), o.getGoodsId(), o.getInId());
                allocateMapper.updateOutNum(o.getNum(), o.getGoodsId(), o.getOutId());
            });
        } catch (Exception e) {
            throw new BusinessException("调出仓库库存不足!");
        }
    }

    @Override
    public int batchStatus(AuditStatus auditStatus) {
        return allocateMapper.batchStatus(auditStatus);
    }
}
